﻿using Abp.AspNetCore.Mvc.Controllers;
using Abp.Domain.Repositories;
using Batman.Jianbei.Forum.Controllers.H5.Responses;
using Batman.Jianbei.Forum.Core.Domains.Dicts;
using Batman.Jianbei.Forum.Core.Domains.Pumps;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Batman.Jianbei.Forum.Controllers.H5
{
    public class PumpInfoSearchController : AbpController
    {
        public IRepository<PumpSaleInfo, string> repository { get; set; }

        public IRepository<PumpBrand, string> InsulinPumpRepo { get; set; }

        public IRepository<PumpModel, string> InsulinPumpModelRepo { get; set; }

        /// <summary>
        /// H5端输入泵号或手机号码查看泵号信息
        /// </summary>
        /// <param name="phoneOrPumpNo"></param>
        /// <returns></returns>
        public IActionResult DoSearch(string phoneOrPumpNo)
        {
            var query = repository.GetAll().AsNoTracking();

            query = query.Where(t => t.PumpNo == phoneOrPumpNo || t.Phone == phoneOrPumpNo);

            query = query.Include(t => t.PumpModel).ThenInclude(t => t.PumpBrand);

            var results = query.Select(t => new PumpInfoSearchResponse
            {
                CustomerName = t.CustomerName,
                PumpBrandName = t.PumpModel.PumpBrand.Name,
                PumpModelName = t.PumpModel.Name,
                Phone = t.Phone,
                PumpNo = t.PumpNo,
                WarrantyStart = t.WarrantyStart,
                WarrantyEnd = t.WarrantyEnd
            }).ToList();

            return Json(results);
        }
    }
}
